﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace InSys.Documents
{
    public partial class FormPerson : Form
    {
        private int id_person;
        private MapTestLinqDataContext db = new MapTestLinqDataContext(Properties.Settings.Default.testlinqConnectionString);
        public FormPerson()
        {
            InitializeComponent();
            InitCommonDataSources();
        }

        public FormPerson(int id_person)
        {
            InitializeComponent();
            InitCommonDataSources();
            this.id_person = id_person;
            if (0 == id_person)
                CreateNew();
            else
                LoadInfoData(id_person);
        }

        private void LoadInfoData(int id_person)
        {
//            MessageBox.Show(id_person.ToString());
            var query = from item in db.persons where item.id_person == id_person select item;
            personBindingSource.DataSource = query;
        }

        private void CreateNew()
        {
            MessageBox.Show("Create new person");
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            Validate();
            db.SubmitChanges();
            Close();
        }

        private void InitCommonDataSources()
        {
            var query = from item in db.departments select item;
            departmentBindingSource.DataSource = query;
            labelActualDataValue.Text = Program.gWorkDate.ToShortDateString();

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void person_departmentDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

    }
}
